/*
** gamer.c for Gamer in /home/vriely/Dropbox/work/Unix/Zappy_rendu/serv-src
**
** Made by
** Login   <@epitech.net>
**
** Started on  Thu Jun 07 20:58:01 2012
** Last update Thu Jul 19 17:43:09 2012 pierrick druart
*/

#include		<string.h>
#include		<stdlib.h>
#include		<stdio.h>

#include		"server.h"

struct s_gamer		*new_gamer(t_team *team, int maxx, int maxy)
{
  struct s_gamer	*gamer;

  gamer = (t_gamer *) malloc(sizeof(t_gamer));
  if (gamer != NULL)
  {
    gamer->task = new_list();
    gamer->team = team;
    gamer->food = 0;
    gamer->action = 0;
    gamer->direction = NORD;
    memset(gamer->inventory, 0, sizeof(gamer->inventory));
    gamer->inventory[FOOD] = 10;
    gamer->level = 1;
    gamer->coord.x = rand() % maxx;
    gamer->coord.y = rand() % maxy;

    gamer->action_str = NULL;
  }
  return (gamer);
}

struct s_gamer		*new_gamer_init(t_team *team, t_coord coord)
{
  struct s_gamer	*gamer;

  gamer = (t_gamer *) malloc(sizeof(t_gamer));
  if (gamer != NULL)
  {
    gamer->task = new_list();
    gamer->team = team;
    gamer->food = 0;
    gamer->action = 0;
    gamer->direction = NORD;
    memset(gamer->inventory, 0, sizeof(gamer->inventory));
    gamer->inventory[FOOD] = 10;
    gamer->level = 1;
    gamer->coord.x = coord.x;
    gamer->coord.y = coord.y;
    gamer->action_str = NULL;
  }
  return (gamer);
}

char			check_food(t_game *game, t_client *client,
    long unsigned int elapsed_time)
{
  while (elapsed_time > client->gamer->food)
  {
    elapsed_time -= client->gamer->food;
    client->gamer->food = FOOD_TIMER(game->time);
    client->gamer->inventory[FOOD]--;
  }
  client->gamer->food -= elapsed_time;
  if (client->gamer->inventory[FOOD] < 0)
  {
    client->send->concat(client->send, "mort\n");
    serv_monitor("pdi", client, game, game->graphic);
    return (0);
  }
  return (1);
}
